import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import fromItem from '@/components/from-item/from-item.vue'
import wx from "weixin-js-sdk";
import lsjUpload from '@/uni_modules/lsj-upload/components/lsj-upload/lsj-upload.vue';
import {
	log
} from 'util'
export default {
	props: {
		mustIcon: {
			type: String,
			default: require('@/static/Scope.png'),
		},
		unIcon: {
			type: String,
			default: require('@/static/unscope.png'),
		}
	},
	data() {
		return {
			option: {},
			heights: '156rpx',
			size: 10,
			debug: true,
			// 选择文件后是否立即自动上传
			instantly: true,
			// 微信小程序Map对象for循环不显示，所以转成普通数组，不要问为什么，我也不知道
			count: 8,

			Inv: 1,
			bihe: false, //闭合是否禁用
			status: null,
			yiji: false,
			erji: false,
			statusNum: null, //调用那个保存接口

			positionData: [], //存放位置
			page: false,
			click: '',
			appliance: null,
			newDisabled: false, //基本信息是否可编辑
			newMust: "must", //基本信息必填
			flagNum: -1,
			// check:false,//处理是否可编辑
			gridmanList: [], //所有网格员
			selector: "text",
			date1: "date1",
			// 弹框
			selectParent: true,
			multiple: false,
			companyTree: [], //树结构	
			TreeNum: null, //第几个树结构
			flod: true,

			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			// loading样式
			closeFormData: {
				fId: "", // 申请单fid //为空判断为新增
				fCompanyid: "", // 施工标段id
				fParentid: "", // 对应的超标记录fid
				// fWarningcontent: "", // 预警内容
				fReason: "", // 问题原因
				fHandleway: "", // 处理方式
				fHandleresult: "", // 处理结果
				fHandledates: "", // 处理日期
				fHandleusername: "", // 处理人名称
				fHandlefile: "", // 附件
				aduitStatus:""


			},
			SauditFileurllist: [],
			FauditFileurllist: [],
			FauditFormData: {
				fId: "", // 申请单fid //为空判断为新增
				fCompanyid: "", // 施工标段id
				fParentid: "", // 对应的超标记录fid


				fFirstcheckresult: "", // 一级审核处理结果
				fFirstcheckopinion: "", // 一级审核处理意见
				fFirstcheckusername: "", // 一级审核处理人name
				fFirstcheckdates: "", // 一级审核处理日期
				fFirstcheckfile: "", // 一级审核附件
				aduitStatus:""

			},
			closeFileurllist: [],
			SauditFormData: {
				fId: "", // 申请单fid //为空判断为新增
				fCompanyid: "", // 施工标段id
				fParentid: "", // 对应的超标记录fid
				
				fSecondcheckresult: "", // 二级审核处理结果
				fSecondcheckopinion: "", // 二级审核处理意见
				fSecondcheckusername: "", // 二级审核处理人name
				fSecondcheckdates: "", // 二级审核处理日期
				fSecondcheckfile: "", // 二级审核附件
				aduitStatus:"",
			},

			ifArr: ['同意', '不同意'],
			totalData: {}, // 详情
			flagNum: -1,
			flagNum2: -1,

			userInfo: null,
			must: "must",
			disabled: false,
			recordfiles: [], //删除文件集合
			optionsF: null, //上个页面传递过来的参数
			timing: null, //当前年月日
			//显示隐藏
			basic: 1, //基本信息
			close: 1, //问题处理情况
			timing: null,
			btnShow: null


		};
	},
	onLoad(options) {

		var that = this;
		this.status = JSON.parse(options.fId).aduitStatus
		console.log(1111, options, this.status);

		var newVal = JSON.parse(options.fId)

		if (newVal.fCheckstatus == 0) {
			this.statusNum = "1"
		} else if (newVal.fCheckstatus == 1) {
			this.statusNum = "2"
		} else if (newVal.fCheckstatus == 2) {
			this.statusNum = "3"
		} else if (newVal.fCheckstatus == 3) {
			this.statusNum = "3"
		}
		console.log(555555, this.statusNum);
		uni.getStorage({ //监督检查方式
			key: 'userInfo',
			success: function(res) {
				that.userInfo = JSON.parse(res.data);

				that.closeFormData.fHandleusername = that.userInfo.realName //入库人员
				that.closeFormData.userName = that.userInfo.realName //入库人员
				that.closeFormData.userId = that.userInfo.userId //入库人员id
				that.closeFormData.fCompanyid = that.userInfo.companyId;
				// 一级审核
				that.FauditFormData.fFirstcheckusername = that.userInfo.realName //入库人员
				that.FauditFormData.userName = that.userInfo.realName //入库人员
				that.FauditFormData.userId = that.userInfo.userId //入库人员id
				that.FauditFormData.fCompanyid = that.userInfo.companyId;

				that.SauditFormData.fSecondcheckusername = that.userInfo.realName //入库人员
				that.SauditFormData.userName = that.userInfo.realName //入库人员
				that.SauditFormData.userId = that.userInfo.userId //入库人员id
				that.SauditFormData.fCompanyid = that.userInfo.companyId;



				// that.optionsF = options;




				that.getDetailData(options.id, JSON.parse(options.fId))

			}
		});
		// || MP-WEIXIN || H5
		// #ifdef APP-PLUS
		this.playVideo()
		uni.$on('drawer-page', (data) => {
			that.clickNum(data)
		})
		// #endif
		// #ifdef H5
		this.appliance = 1
		// that.geterweimaInfo()
		// #endif
	},
	onShow() {

	},
	components: {
		loading,
		cLabel,
		fromItem,
	},

	onUnload() {
		uni.$off('drawer-page')
	},
	onReady() {

		this.$nextTick(() => {
			this.$refs.lsjUpload.show();
		})
		var that = this
		if (this.statusNum == 1) {


			if (that.totalData.ffHandlefile == "" && that.totalData.fHandlefile == null && that.totalData
				.fHandlefile ==
				undefined) {
				console.log("文件么准备好")
			} else {
				setTimeout(() => {

					uni.getStorage({
						key: 'userInfo',
						success: function(res) {
							that.userInfo = JSON.parse(res.data)
							var folderid = that.totalData.fHandlefile
							var userId = that.userInfo.userId
							var userName = that.userInfo.realName
							console.log(userId)
							var url = that.$http.upload + '?folderid=' + folderid + '&userId=' +
								userId + '&userName=' +
								userName;
							that.option = {
								// 上传服务器地址，此地址需要替换为你的接口地址
								url: url,
								// 上传附件的key
								name: 'file',
								header: {},
								formData: {}
							};
						}
					})
				}, 3000)
			}
		} else if (this.statusNum == 2) {

			// FauditFormData.fCheckfileurl1
			if (that.totalData.fFirstcheckfile == "" && that.totalData.fFirstcheckfile == null && that.totalData
				.fFirstcheckfile ==
				undefined) {
				console.log("文件么准备好")
			} else {
				setTimeout(() => {

					uni.getStorage({
						key: 'userInfo',
						success: function(res) {
							that.userInfo = JSON.parse(res.data)
							var folderid = that.totalData.fFirstcheckfile
							var userId = that.userInfo.userId
							var userName = that.userInfo.realName
							console.log(userId)
							var url = that.$http.upload + '?folderid=' + folderid + '&userId=' +
								userId + '&userName=' +
								userName;
							that.option = {
								// 上传服务器地址，此地址需要替换为你的接口地址
								url: url,
								// 上传附件的key
								name: 'file',
								header: {},
								formData: {}
							};
						}
					})
				}, 3000)
			}
		} else if (this.statusNum == 3) {

			// FauditFormData.fCheckfileurl1
			if (that.totalData.fSecondcheckfile == "" && that.totalData.fSecondcheckfile == null && that.totalData
				.fSecondcheckfile ==
				undefined) {
				console.log("文件么准备好")
			} else {
				setTimeout(() => {

					uni.getStorage({
						key: 'userInfo',
						success: function(res) {
							that.userInfo = JSON.parse(res.data)
							var folderid = that.totalData.fSecondcheckfile
							var userId = that.userInfo.userId
							var userName = that.userInfo.realName
							console.log(userId)
							var url = that.$http.upload + '?folderid=' + folderid + '&userId=' +
								userId + '&userName=' +
								userName;
							that.option = {
								// 上传服务器地址，此地址需要替换为你的接口地址
								url: url,
								// 上传附件的key
								name: 'file',
								header: {},
								formData: {}
							};
						}
					})
				}, 3000)
			}
		}
	},
	methods: {

		ViewImage(e, url) {
			var photo = []
			var file = null;
			if (this.statusNum == 1) {
				file = this.closeFileurllist
			} else if (this.statusNum == 2) {
				file = this.FauditFileurllist
			} else if (this.statusNum == 3) {
				file = this.SauditFileurllist
			}

			file.map(item => {
				if (item.fFileType != '.pdf') {
					photo.push(item.url)
				}
			})
			console.log(e, url)
			uni.previewImage({
				urls: photo,
				current: url,
				loop: true,
				indicator: 'default'
			});
		},
		pdfPage(name, url) {
			var aa =
				url
			let links = encodeURIComponent(aa)

			uni.navigateTo({
				url: '/pages/apply/pdf?links=' + links + '&names=' + name,
			})

		},
		// 上传进度回调
		onprogress(item) {
			var that = this
			if (item.responseText) {
				var data = JSON.parse(item.responseText)
				console.log(444, data, data[0].atta.fFiletype)
				var url = data[0].atta.fFilepath
				var urlPath;
				console.log(99, that.statusNum, url)
				if (url.indexOf("D:") != -1) {
					urlPath = url.replace(that.$http.redf, that.$http.file)


					if (that.statusNum == 1) {
						that.closeFileurllist.push({
							url: urlPath,
							fFileType: data[0].atta.fFiletype,
							name: item.name,
							fId: data[0].atta.fId
						})
					} else if (that.statusNum == 2) {
						that.FauditFileurllist.push({
							url: urlPath,
							fFileType: data[0].atta.fFiletype,
							name: item.name,
							fId: data[0].atta.fId
						})
					} else if (that.statusNum == 3) {
						that.SauditFileurllist.push({
							url: urlPath,
							fFileType: data[0].atta.fFiletype,
							name: item.name,
							fId: data[0].atta.fId
						})
					}
					console.log('闭合:', that.closeFileurllist, '一级:', that.FauditFileurllist, '二级:', that
						.SauditFileurllist)
				}

				this.$refs.loading.close();
			}
		},
		// 文件选择回调
		onChange(files) {
			this.$refs.loading.open();

		},
		// 删除文件
		DelFile(e, id, index) {
			console.log(e, id, index);
			var that = this
			uni.showModal({
				title: '召唤师',
				content: '确定要删除这段回忆吗？',
				cancelText: '再看看',
				confirmText: '再见',
				success: res => {
					if (res.confirm) {





						if (this.statusNum == 1) {
							that.closeFormData.recordfiles.push({
								fId: id
							})
							this.closeFileurllist.splice(index, 1)
						} else if (this.statusNum == 2) {
console.log(that.FauditFormData.fFirstcheckdates);
							that.FauditFormData.recordfiles.push({
								fId: id
							})
							this.FauditFileurllist.splice(index, 1)
						} else if (this.statusNum == 3) {
							that.SauditFormData.recordfiles.push({
								fId: id
							})
							this.SauditFileurllist.splice(index, 1)
						}


					}
				}
			})
		},

		yj_searchForm(num) {
			var that = this
			this.Inv = num;
			console.log(num)
		},


		clickNum(data) {
			this.saveData(this.statusNum);
		},
		getNewData() {
			var that = this;
		},
		// 详情
		getDetailData(id, newVal) {
			console.log(1, newVal);

			this.getDate();
			var that = this;
			that.closeFileurllist = [];
			var datas = {
				fId: id,

			}
			that.$http.getSteelProtectionTestTaskData(datas).then(res => {
				if (res.errcode == 0) {
					var data = res.data.testTaskclose;

					that.totalData = res.data.testTaskclose;
					that.closeFormData.fId = data.fId
					that.FauditFormData.fId = data.fId
					that.SauditFormData.fId = data.fId
					// that.closeFormData.fNonresult = data.fNonresult
					that.closeFormData.fParentid = id; //按钮 
					that.FauditFormData.fParentid = id; //按钮 
					that.FauditFormData.fParentid = id; //按钮 
					that.closeFormData.fProcessfileurl = data.fProcessfileurl

					this.closeFormData.fNonresult = data.fNonresult
					this.closeFormData.fHandlefile = data.fHandlefile; //  闭合
					this.closeFormData.fFirstcheckfile = data.fFirstcheckfile; //  一级审核
					this.closeFormData.fSecondcheckfile = data.fSecondcheckfile; //  一级审核


					if (data.fHandledates == null || data.fHandledates == "") {
						this.closeFormData.fHandledates = this.timing
						this.closeFormData.fHandleusername = this.userInfo.realName; //处理人
						this.closeFormData.fWarningcontent = data.fWarningcontent

					} else {
						this.closeFormData = data
						this.closeFormData.recordfiles = []
						that.closeFileurllist = res.data.fCloseSourcefileList
					}

					// // 一级审核
					if (data.fFirstcheckdates == null || data.fFirstcheckdates == "") {
						this.FauditFormData.fFirstcheckdates = this.timing
						this.FauditFormData.fFirstcheckusername = this.userInfo.realName; //一级审核人姓名
					} else {
						this.FauditFormData = data
						this.FauditFormData.recordfiles = []
						
						this.FauditFileurllist = res.data.fFirstSourcefileList

						this.flagNum = 0
					}
					// // 二级审核
					if (data.fSecondcheckdates == null || data.fSecondcheckdates == "") {
						this.SauditFormData.fSecondcheckdates = this.timing
						this.SauditFormData.fSecondcheckusername = this.userInfo.realName; //一级审核人姓名
					} else {
						this.SauditFormData = data

						this.SauditFormData.recordfiles = []
						this.SauditFileurllist = res.data.fSecondSourcefileList
						this.flagNum2 = 0
					}



					that.page = true
					this.getExamine(newVal)

				} else {
					uni.showToast({
						title: "请求失败",
						icon: 'none',
						duration: 2000
					});
				}

			}, (err) => {
				uni.showToast({
					title: "接口报错",
					icon: 'none',
					duration: 2000
				});
			})

		},
		getExamine(data) {
			var that = this
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					that.userInfo = JSON.parse(res.data)

					var status = data.fCheckstatus;
					that.$http.htgetStationWarnByCompanyId({
							fCompanyid: data.fCompanyid,
						})
						.then((res) => {
							if (res.errcode == 0) {

								 var arr = res.data.yjQmpMsCheckEntitywarn.fAduitUser2.split("-");
console.log(arr,99);
								uni.getStorage({ //监督检查方式
									key: 'userInfo',
									success: function(res) {
										that.userInfo = JSON.parse(res.data);
										that.btnShow = true;
										if ((that.userInfo.companyNature == arr[status])) {
											if (status == 0) {
												// 闭合申请单 可编辑
												that.Inv = 1;
												that.bihe = false;
												that.yiji = true;
												that.erji = true;

											} else if (status == 1) {
												that.Inv = 2;
												that.bihe = true;
												that.yiji = false;
												that.erji = true;


											} else if (status == 2) {
												that.Inv = 3;
												that.bihe = true;
												that.yiji = true;
												that.erji = false;

											} else if (status == 3) {
												that.Inv = 3;
												that.btnShow = false
												that.bihe = true;
												that.yiji = true;
												that.erji = true;
											}
										} else {
											that.btnShow = false
											that.bihe = true;
											that.yiji = true;
											that.erji = true;

											if (status == 0) {
												that.Inv = 1;
												//  二级闭合
											} else if (status == 1) {

												that.Inv = 2;
											} else if (status == 2) {
												that.Inv = 3;

											} else if (status == 3) {
												that.Inv = 3;
											}
										}
										console.log(status, 888);
										that.closeFormData.aduitStatus = status
										that.FauditFormData.aduitStatus = status
										that.SauditFormData.aduitStatus = status
									}
								});
							}

						})

				}
			})

		},

		// 下拉框
		selectorFun(e) {
			console.log("下拉选择", e);

		},

		// 判断必填项
		saveData(data) {
			console.log('000', this.closeFormData, 11, this.FauditFormData, 222, this.SauditFormData);
			var that = this;
			// var num = 0
			if (data == 1) {

				if (this.closeFormData.fWarningcontent == "" || this.closeFormData.fWarningcontent == undefined) {
					uni.showToast({
						title: '预警内容不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (this.closeFormData.fReason == "" || this.closeFormData.fReason == undefined) {
					uni.showToast({
						title: '异常原因不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (this.closeFormData.fHandleway == "" || this.closeFormData.fHandleway == undefined) {
					uni.showToast({
						title: '处理方式不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (this.closeFormData.fHandleusername == "" || this.closeFormData.fHandleusername ==
					undefined) {
					uni.showToast({
						title: '处理人不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (this.closeFormData.fHandledates == "" || this.closeFormData.fHandledates == undefined) {
					uni.showToast({
						title: '处理日期不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else {


					that.$refs.loading.open();

					this.$http.addClosedForm(this.closeFormData).then(res => {
						if (res.errcode == 0) {
							// that.$refs.loading.close();
							that.page = false
							if (!that.click) {
								setTimeout(() => {
									uni.showToast({
										title: "保存成功",
										icon: 'none',
										duration: 2000
									});
									uni.redirectTo({
										url: "/pages/apply/rebarThicknessManage/rebarUnqualifiedData/testing/index",
										success: () => {
											that.clickN = 1
											that.click = false
											that.$refs.loading.close();
											// #ifdef H5
											setTimeout(() => {
												that.click = false
											}, 100)
											// #endif
										}
									})
								}, 1500)
							}

							that.click = true

						} else {
							console.log("接口报错")
							that.$refs.loading.close();
							that.click = true
						}
					}, (err) => {
						that.$refs.loading.close();
						that.click = true
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					})
				}
			} else if (data == 2) {
				if (this.FauditFormData.fFirstcheckresult == "" || this.FauditFormData.fFirstcheckresult == undefined) {
					uni.showToast({
						title: '请选择审核结果',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (this.FauditFormData.fFirstcheckopinion == "" || this.FauditFormData.fFirstcheckopinion ==
					undefined) {
					uni.showToast({
						title: '审核意见不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else {


					that.$refs.loading.open();

					// console.log(num)
					//
					this.$http.addClosedForm(this.FauditFormData).then(res => {
						if (res.errcode == 0) {
							// that.$refs.loading.close();
							that.page = false
							if (!that.click) {
								setTimeout(() => {
									uni.showToast({
										title: "保存成功",
										icon: 'none',
										duration: 2000
									});
									uni.redirectTo({
										url: "/pages/apply/rebarThicknessManage/rebarUnqualifiedData/testing/index",
										success: () => {
											that.clickN = 1
											that.click = false
											that.$refs.loading.close();
											// #ifdef H5
											setTimeout(() => {
												that.click = false
											}, 100)
											// #endif
										}
									})
								}, 1500)
							}

							that.click = true

						} else {
							console.log("接口报错")
							that.$refs.loading.close();
							that.click = true
						}
					}, (err) => {
						that.$refs.loading.close();
						that.click = true
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					})
				}
			} else if (data == 3) {
				if (this.SauditFormData.fSecondcheckresult == "" || this.SauditFormData.fSecondcheckresult ==
					undefined) {
					uni.showToast({
						title: '请选择审核结果',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (this.SauditFormData.fSecondcheckopinion == "" || this.SauditFormData.fSecondcheckopinion ==
					undefined) {
					uni.showToast({
						title: '审核意见不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else {


					that.$refs.loading.open();

					// console.log(num)
					//
					this.$http.addClosedForm(this.SauditFormData).then(res => {
						if (res.errcode == 0) {
							// that.$refs.loading.close();
							that.page = false
							if (!that.click) {
								setTimeout(() => {
									uni.showToast({
										title: "保存成功",
										icon: 'none',
										duration: 2000
									});
									uni.redirectTo({
										url: "/pages/apply/rebarThicknessManage/rebarUnqualifiedData/testing/index",
										success: () => {
											that.clickN = 1
											that.click = false
											that.$refs.loading.close();
											// #ifdef H5
											setTimeout(() => {
												that.click = false
											}, 100)
											// #endif
										}
									})
								}, 1500)
							}

							that.click = true

						} else {
							console.log("接口报错")
							that.$refs.loading.close();
							that.click = true
						}
					}, (err) => {
						that.$refs.loading.close();
						that.click = true
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					})
				}
			}

		},


		// 原生窗体按钮显示
		playVideo() {
			let subNVue = uni.getSubNVueById('safe_notice')
			subNVue.show()
		},

		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			const hh = (time.getHours() + "").padStart(2, "0");
			const mm = (time.getMinutes() + "").padStart(2, "0");
			const ss = (time.getSeconds() + "").padStart(2, "0");

			var timer = `${y}-${m}-${d} ${hh}:${mm}`;
			var timing = `${y}-${m}-${d}`;
			this.timing = timing
			this.closeFormData.fHandledates = `${y}-${m}-${d}`;
			this.FauditFormData.fFirstcheckdates = `${y}-${m}-${d}`; //一级审核日期
			this.SauditFormData.fSecondcheckdates = `${y}-${m}-${d}`; //一级审核日期
		},
		callback() {}
	}
}
